package com.py.credit.utils;

import java.net.URLEncoder;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baidu.aip.util.Base64Util;
import com.py.framework.core.redis.bis.JedisBisManage;

public class IdCardCheckUtil {

	    public static String checkIdCard(String filePath) {
	    	String idcard = "";
	        // 身份证识别url
	        String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
	        // 本地图片路径
	        try {
	            byte[] imgData = FileBUtil.readFileByBytes(filePath);
	            String imgStr = Base64Util.encode(imgData);
	            // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
	            String params = "id_card_side=front&" + URLEncoder.encode("image", "UTF-8") + "="
	                    + URLEncoder.encode(imgStr, "UTF-8");
	            /**
	             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
	             */
	            String accessToken = JedisBisManage.getJedis().get("access_token");
	            String result = HttpUtil.post(idcardIdentificate, accessToken, params);
	            Map<String, Object> map = JSONObject.parseObject(result, new TypeReference<Map<String,Object>>(){});
	           Map<String,Map<String,Object>> data = (Map<String, Map<String, Object>>) map.get("words_result");
	           idcard = (String) data.get("公民身份号码").get("words");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return idcard;
	    }
}
